package org.dtrd.modules.ocr.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.modules.ocr.entity.po.DtrdEntRdOcrMapping;
import org.dtrd.modules.ocr.mapper.DtrdEntRdOcrMappingMapper;
import org.dtrd.modules.ocr.service.IDtrdEntRdOcrMappingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.ocr.entity.bean.OcrMappingDetail;
import org.dtrd.modules.ocr.entity.bean.OcrMappingListInfo;
import org.dtrd.modules.ocr.entity.bean.OcrMappingListRequest;
import org.dtrd.modules.ocr.entity.bean.OcrMappingPageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-09-06
 */
@DS("multi-datasource1")
@Service
@Slf4j
public class DtrdEntRdOcrMappingServiceImpl extends ServiceImpl<DtrdEntRdOcrMappingMapper, DtrdEntRdOcrMapping> implements IDtrdEntRdOcrMappingService {

    private DtrdEntRdOcrMappingMapper dtrdEntRdOcrMappingMapper;

    @Override
    public List<OcrMappingDetail> getOcrMappingList(OcrMappingListRequest request) {

        List<String> words = request.getWords();

        List<OcrMappingDetail> ocrMappingList = new ArrayList<>(words.size());
        if (CollectionUtils.isNotEmpty(words)) {
            OcrMappingDetail ocrMapping;
            for (String word : words) {
                log.info(word);
                ocrMapping = this.getOcrMapping(word);
                ocrMappingList.add(ocrMapping);
            }
        }
        return ocrMappingList;
    }


    @Override
    public OcrMappingDetail getOcrMapping(String word) {
        LambdaQueryWrapper<DtrdEntRdOcrMapping> queryWrapper = baseQueryWrapper();
        queryWrapper.eq(DtrdEntRdOcrMapping::getWord, word).last("limit 1");
        DtrdEntRdOcrMapping dtrdEntRdOcrMapping = getOne(queryWrapper);
        log.info("dtrdEntRdOcrMapping"+ dtrdEntRdOcrMapping);
        return Optional.ofNullable(dtrdEntRdOcrMapping)
                .map(po -> new OcrMappingDetail().parseFromPo(po))
                .orElse(new OcrMappingDetail().init(word));
    }

    @Override
    public boolean saveOrUpdateOcrMappingByWord(OcrMappingDetail ocrMappingDetail) {

        return saveOrUpdate(new DtrdEntRdOcrMapping().parseFromDto(ocrMappingDetail));
    }

    private LambdaQueryWrapper<DtrdEntRdOcrMapping> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdOcrMapping.class)
                        .eq(DtrdEntRdOcrMapping::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdOcrMapping> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdOcrMapping.class)
                        .eq(DtrdEntRdOcrMapping::getIsDel, 0);
    }
}
